// 登录接口 - 使用假数据
export default defineEventHandler(async (event) => {
  const body = await readBody(event)
  const { email, password } = body

  // 模拟延迟
  await new Promise(resolve => setTimeout(resolve, 500))

  // 测试用户数据
  const testUsers = [
    {
      id: '1',
      username: 'admin',
      email: 'admin@example.com',
      password: '123456',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=admin',
      role: 'admin'
    },
    {
      id: '2',
      username: 'testuser',
      email: 'test@example.com',
      password: '123456',
      avatar: 'https://api.dicebear.com/7.x/avataaars/svg?seed=test',
      role: 'user'
    }
  ]

  // 查找用户
  const user = testUsers.find(u => u.email === email && u.password === password)

  if (!user) {
    throw createError({
      statusCode: 401,
      message: '邮箱或密码错误'
    })
  }

  // 生成假的 token
  const token = `token_${user.id}_${Date.now()}`
  const refreshToken = `refresh_${user.id}_${Date.now()}`

  return {
    code: 200,
    message: '登录成功',
    data: {
      token,
      refreshToken,
      user: {
        id: user.id,
        username: user.username,
        email: user.email,
        avatar: user.avatar,
        role: user.role
      }
    }
  }
})

